#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>

class Solution {
public:
    void sortColors(vector<int>& nums)
    {
        int n = nums.size();
        int left = -1, right = n;
        int cur = 0;

        while (cur < right)
        {
            if (nums[cur] == 0) swap(nums[cur++], nums[++left]);
            else if (nums[cur] == 1) cur++;
            else swap(nums[cur], nums[--right]);
        }
    }
};